package com.youku.adSDK.view
{
   import flash.display.MovieClip;
   import com.youku.adSDK.ADEventDispather;
   import com.youku.adSDK.view.components.PauseAD;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.model.vo.ADData;
   import com.youku.adSDK.YoukuAdEvent;
   import com.youku.adSDK.view.events.FlashADEvent;
   
   public class PauseMediator extends Object
   {
      
      public function PauseMediator(param1:ADEventDispather, param2:ADContext) {
         super();
         this._mainMc = param2.mainMc;
         this._context = param2;
         this._eventDispather = param1;
         this._isPaused = false;
      }
      
      private var _mainMc:MovieClip;
      
      private var _eventDispather:ADEventDispather;
      
      private var _pause:PauseAD;
      
      private var _isPaused:Boolean;
      
      private var _width:Number = 0;
      
      private var _height:Number = 0;
      
      private var _barH:Number = 0;
      
      private var _shutData:Boolean = false;
      
      private var _haveToolBox:Boolean = false;
      
      private var _context:ADContext;
      
      public function init() : void {
         this._eventDispather.addEventListener(ADEvent.PLAYER_PAUSE,this.onPlayerPause);
         this._eventDispather.addEventListener(ADEvent.PLAYER_RESUME,this.onPlayerResume);
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
         this._eventDispather.addEventListener(ADEvent.COMMON_RESET,this.onPlayerReset);
         this._eventDispather.addEventListener(ADEvent.REQUEST_AND_PLAY_BACK_AD,this.onPlayerShowBackAD);
         this._eventDispather.addEventListener(ADEvent.PANEL_STATE_CHANGE,this.onPlayerEvent);
         this._eventDispather.addEventListener(ADEvent.PAUSE_DATA_LOADED,this.onADEvent);
         this._eventDispather.addEventListener(ADEvent.PAUSE_DATA_LOAD_ERROR,this.onADEvent);
      }
      
      private function onADEvent(param1:ADEvent) : void {
         if(this._shutData)
         {
            return;
         }
         switch(param1.type)
         {
            case ADEvent.PAUSE_DATA_LOADED:
               this.initPauseAD(param1.data as ADData);
               break;
            case ADEvent.PAUSE_DATA_LOAD_ERROR:
               if((this._isPaused) && !this._haveToolBox && (this._context.winType == "interior" || this._context.winType == "exterior"))
               {
                  this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_PAUSE_NULL,{"from":"pauseAD"}));
               }
               break;
         }
      }
      
      private function onPlayerPause(param1:ADEvent) : void {
         if(this._context.isTudouPlayer)
         {
            return;
         }
         if((param1.data.useraction) && !this._haveToolBox)
         {
            this._isPaused = true;
            this._shutData = false;
            this._eventDispather.dispatchEvent(new ADEvent(ADEvent.DO_PAUSE));
         }
      }
      
      private function onPlayerResume(param1:ADEvent) : void {
         if(this._isPaused)
         {
            this.removeAD();
            this._isPaused = false;
            this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_PAUSE_HIDE,{"from":"pauseAD"}));
         }
      }
      
      private function onPlayerEvent(param1:ADEvent) : void {
         this._haveToolBox = param1.data.havePanel;
         if(this._haveToolBox)
         {
            if(this._pause)
            {
               this._pause.visible = false;
            }
         }
         else if(this._pause)
         {
            this._pause.visible = true;
         }
         
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._width = param1.data.width;
         this._height = param1.data.height;
         this._barH = param1.data.cheight;
      }
      
      private function onPlayerReset(param1:ADEvent) : void {
         this._shutData = true;
         this.removeAD();
         this._isPaused = false;
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_PAUSE_HIDE,{"from":"pauseAD"}));
      }
      
      private function onPlayerShowBackAD(param1:ADEvent) : void {
         this._isPaused = false;
         if(this._pause)
         {
            this._pause.dispose();
         }
      }
      
      private function initPauseAD(param1:ADData) : void {
         if(!this._isPaused)
         {
            return;
         }
         this.removeAD();
         this._pause = new PauseAD();
         this._mainMc.addChild(this._pause);
         this._pause.addEventListener(FlashADEvent.CONTENT_LOADED,this.onPauseContentLoaded);
         this._pause.addEventListener(FlashADEvent.CONTENT_LOAD_ERROR,this.onPauseContentLoadError);
         if(!this._width)
         {
            this._width = this._mainMc.stage.stageWidth;
            this._height = this._mainMc.stage.stageHeight;
            this._barH = 40;
         }
         var _loc2_:Boolean = this._context.isInFullScreen;
         var _loc3_:Number = 400;
         var _loc4_:Number = 300;
         if(!(param1.W == 0) && !(param1.H == 0))
         {
            _loc3_ = param1.W;
            _loc4_ = param1.H;
         }
         this._pause.init(param1,_loc3_,_loc4_,_loc2_,this._width,this._height,this._barH,this._context,this._eventDispather);
         if(this._haveToolBox)
         {
            this._pause.visible = false;
         }
      }
      
      private function onPauseContentLoaded(param1:FlashADEvent) : void {
      }
      
      private function onPauseContentLoadError(param1:FlashADEvent) : void {
         this._pause.dispose();
      }
      
      private function removeAD() : void {
         if((this._isPaused) && (this._pause))
         {
            this._pause.dispose();
            if(this._pause.parent)
            {
               this._mainMc.removeChild(this._pause);
            }
         }
      }
   }
}
